#ifndef LOGICCOUNTER_H
#define LOGICCOUNTER_H

#include "LogicPool/LogicPool.h"

class LogicCounter : public LogicPool 
{
  protected:
  
    int mode;
    
  public:

    static const int MODE_COUNTER      = 0;
    static const int MODE_PULSER       = 1;
    static const int MODE_ASYNC_PULSER = 2;

    LogicCounter(LogicInterface & iinterface, uint8_t number = 1) 
        : LogicPool(iinterface, 'C', number), mode(0) {};

    void SetMode(int imode)
    {
        SetAddress(2);
        logicpool.WriteByte(mode = imode);
    };

    void ClearCounter() 
    {
        SetMode(mode);
    };

    uint32_t GetCounter() 
    {
        SetAddress(2);
        return logicpool.ReadLong();
    };

    void SetMark(uint8_t no, uint32_t value)
    {
        SetAddress(3 + no);
        logicpool.WriteLong(value);
    };

    void SetPulserFrequency(uint32_t freq)
    {
        const unsigned int clock_freq = 100000000;
        SetMode(MODE_PULSER);
        SetInput(0, 0); // no trigger signal, just run!
        SetInput(1, 0); // use internal clock
        SetMark(0, clock_freq/freq/2);
        SetMark(1, clock_freq/freq);
    }
};

#endif
